| Conditions | 1 |
| Paths | 1 |
| Total Lines | 160 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 185 | reencryptCredential: function (credential_guid, old_password, new_password) { |
||
| 186 | |||
| 187 | var service = this; |
||
| 188 | |||
| 189 | var progress_datatype = function (current, total, process) { |
||
| 190 | this.process = process; |
||
| 191 | this.current = current; |
||
| 192 | this.total = total; |
||
| 193 | this.calculated = current / total * 100; |
||
| 194 | }; |
||
| 195 | |||
| 196 | var promise_credential_update = function () { |
||
| 197 | service.getCredential(credential_guid).then((function (credential) { |
||
| 198 | this.parent.plain_credential = service.decryptCredential(credential, this.parent.old_password); |
||
| 199 | var tmp = angular.copy(this.parent.plain_credential); |
||
| 200 | |||
| 201 | //@FIXME Your shared credentials are not updated properly |
||
| 202 | if (tmp.hasOwnProperty('shared_key') && tmp.shared_key !== null) { |
||
| 203 | var shared_key = EncryptService.decryptString(angular.copy(tmp.shared_key)).trim(); |
||
| 204 | tmp.shared_key = EncryptService.encryptString(angular.copy(shared_key), this.parent.new_password); |
||
| 205 | tmp.set_share_key = true; |
||
| 206 | tmp.skip_revision = true; |
||
| 207 | this.parent.new_password = shared_key; |
||
| 208 | } |
||
| 209 | |||
| 210 | this.parent.new_credential_cryptogram = service.encryptCredential(tmp, this.parent.new_password); |
||
| 211 | this.call_progress(new progress_datatype(1, 2, 'credential')); |
||
|
|
|||
| 212 | |||
| 213 | // Save data |
||
| 214 | this.parent.new_credential_cryptogram.skip_revision = true; |
||
| 215 | service.updateCredential(this.parent.new_credential_cryptogram, true).then((function () { |
||
| 216 | this.call_progress(new progress_datatype(2, 2, 'credential')); |
||
| 217 | this.call_then({ |
||
| 218 | plain_text: this.parent.plain_credential, |
||
| 219 | cryptogram: this.parent.new_credential_cryptogram |
||
| 220 | }); |
||
| 221 | }).bind(this)); |
||
| 222 | }).bind(this)); |
||
| 223 | }; |
||
| 224 | |||
| 225 | var promise_files_update = function () { |
||
| 226 | // Add the double of the files so we take encryption phase and upload to the server into the math |
||
| 227 | this.total = this.parent.plain_credential.files.length * 2; // Binded on credential finish upload |
||
| 228 | this.current = 0; |
||
| 229 | |||
| 230 | for (var i = 0; i < this.parent.plain_credential.files.length; i++) { |
||
| 231 | var _file = this.parent.plain_credential.files[i]; |
||
| 232 | /* jshint ignore:start */ |
||
| 233 | FileService.getFile(_file).then((function (fileData) { |
||
| 234 | //Decrypt with old key |
||
| 235 | fileData.filename = EncryptService.decryptString(fileData.filename, this.parent.old_password); |
||
| 236 | fileData.file_data = EncryptService.decryptString(fileData.file_data, this.parent.old_password); |
||
| 237 | |||
| 238 | this.current++; |
||
| 239 | |||
| 240 | this.call_progress(new progress_datatype(this.current, this.total, 'files')); |
||
| 241 | |||
| 242 | FileService.updateFile(fileData, this.parent.new_password).then((function () { |
||
| 243 | this.current++; |
||
| 244 | this.call_progress(new progress_datatype(this.current, this.total, 'files')); |
||
| 245 | if (this.current === this.total) { |
||
| 246 | this.call_then('All files has been updated'); |
||
| 247 | } |
||
| 248 | }).bind(this)); |
||
| 249 | }).bind(this)); |
||
| 250 | /* jshint ignore:end */ |
||
| 251 | } |
||
| 252 | if (this.parent.plain_credential.files.length === 0) { |
||
| 253 | this.call_progress(new progress_datatype(0, 0, 'files')); |
||
| 254 | this.call_then("No files to update"); |
||
| 255 | } |
||
| 256 | }; |
||
| 257 | |||
| 258 | var promise_revisions_update = function () { |
||
| 259 | service.getRevisions(this.parent.plain_credential.guid).then((function (revisions) { |
||
| 260 | // Double, so we include the actual upload of the data back to the server |
||
| 261 | this.total = revisions.length * 2; |
||
| 262 | this.upload = 0; |
||
| 263 | this.current = 0; |
||
| 264 | this.revisions = revisions; |
||
| 265 | |||
| 266 | var revision_workload = function () { |
||
| 267 | if (this.revisions.length === 0) { |
||
| 268 | this.call_progress(new progress_datatype(0, 0, 'revisions')); |
||
| 269 | this.call_then("No history to update"); |
||
| 270 | return; |
||
| 271 | } |
||
| 272 | var _revision = revisions[this.current]; |
||
| 273 | //Decrypt! |
||
| 274 | _revision.credential_data = service.decryptCredential(_revision.credential_data, this.parent.old_password); |
||
| 275 | _revision.credential_data = service.encryptCredential(_revision.credential_data, this.parent.new_password); |
||
| 276 | this.current++; |
||
| 277 | |||
| 278 | this.call_progress(new progress_datatype(this.current + this.upload, this.total, 'revisions')); |
||
| 279 | |||
| 280 | service.updateRevision(_revision).then((function () { |
||
| 281 | this.upload++; |
||
| 282 | this.call_progress(new progress_datatype(this.upload + this.current, this.total, 'revisions')); |
||
| 283 | if (this.current + this.upload === this.total) { |
||
| 284 | this.call_then("History updated"); |
||
| 285 | } |
||
| 286 | }).bind(this)); |
||
| 287 | |||
| 288 | if (this.current !== (this.total / 2)) { |
||
| 289 | setTimeout(revision_workload.bind(this), 1); |
||
| 290 | } |
||
| 291 | }; |
||
| 292 | setTimeout(revision_workload.bind(this), 1); |
||
| 293 | }).bind(this)); |
||
| 294 | }; |
||
| 295 | |||
| 296 | var promise_workload = function () { |
||
| 297 | this.old_password = angular.copy(old_password); |
||
| 298 | this.new_password = angular.copy(new_password); |
||
| 299 | this.promises = 0; |
||
| 300 | |||
| 301 | var master_promise = this; |
||
| 302 | |||
| 303 | var password_data = function () { |
||
| 304 | this.old_password = master_promise.old_password; |
||
| 305 | this.new_password = master_promise.new_password; |
||
| 306 | this.plain_credential = master_promise.plain_credential; |
||
| 307 | }; |
||
| 308 | this.credential_data = {}; |
||
| 309 | /** global: C_Promise */ |
||
| 310 | (new C_Promise(promise_credential_update, new password_data())).progress(function (data) { |
||
| 311 | master_promise.call_progress(data); |
||
| 312 | }).then(function (data) { |
||
| 313 | console.warn("End credential update"); |
||
| 314 | master_promise.plain_credential = data.plain_text; |
||
| 315 | master_promise.promises++; |
||
| 316 | |||
| 317 | master_promise.credential_data = data; |
||
| 318 | /** global: C_Promise */ |
||
| 319 | (new C_Promise(promise_files_update, new password_data())).progress(function (data) { |
||
| 320 | master_promise.call_progress(data); |
||
| 321 | }).then(function () { |
||
| 322 | console.warn("End files update"); |
||
| 323 | master_promise.promises--; |
||
| 324 | if (master_promise.promises === 0) { |
||
| 325 | master_promise.call_then(master_promise.credential_data); |
||
| 326 | } |
||
| 327 | }); |
||
| 328 | |||
| 329 | master_promise.promises++; |
||
| 330 | /** global: C_Promise */ |
||
| 331 | (new C_Promise(promise_revisions_update, new password_data())).progress(function (data) { |
||
| 332 | master_promise.call_progress(data); |
||
| 333 | }).then(function () { |
||
| 334 | console.warn("End revisions update"); |
||
| 335 | master_promise.promises--; |
||
| 336 | if (master_promise.promises === 0) { |
||
| 337 | master_promise.call_then(master_promise.credential_data); |
||
| 338 | } |
||
| 339 | }); |
||
| 340 | }); |
||
| 341 | }; |
||
| 342 | /** global: C_Promise */ |
||
| 343 | return new C_Promise(promise_workload); |
||
| 344 | } |
||
| 345 | }; |
||
| 347 | }()); |